package com.bakar.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bakar.service.MessageService;
import com.bakar.service.UserNotOnlineException;
import com.bakar.valueobjects.MessageVO;
import com.bakar.valueobjects.UserVO;

/**
 * Servlet implementation class SendMessage
 */
public class SendMessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SendMessageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO security check
		String currentUser = request.getParameter("currentUser");
		String textMessage = request.getParameter("textMessage");
		String toUser = request.getParameter("toUser");
		
		MessageVO message = new MessageVO(textMessage,toUser,currentUser);
		if(!toUser.contains("bkrgrp")){
			MessageService.setMessageForUser(message);
		} else {
			MessageService.sendMessageToGroup(message);
		}
		
			
		
		PrintWriter out = response.getWriter();
		out.println("SUCESS");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
